define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classroom/course/index' + location.search,
                    add_url: 'classroom/course/add',
                    edit_url: 'classroom/course/edit',
                    del_url: 'classroom/course/del',
                    multi_url: 'classroom/course/multi',
                    import_url: 'classroom/course/import',
                    table: 'classroom_course',
                }
            });

            var table = $("#table");

            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit,.btn-add").data("area", ["95%", "95%"]);
                $(".btn-editone,.btn-edit,.btn-add").data("shade", 0.5);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        {
                            field: 'type.name', title: __('Type.name'), operate: 'LIKE', table: table, class: 'autocontent',
                            formatter: function (value, row, index) {
                                return `<span style="color: #084680;font-weight: bold;">${value}</span> / <span style="color: #1688f1;font-weight: bold;">${row.type2.name}</span>`;
                            }
                        },
                        { field: 'type2.name', title: __('Type2.name'), operate: 'LIKE', visible: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'author.name', title: __('Author.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },

                        { field: 'status', title: __('Status'), searchList: { "1": __('Status 1'), "0": __('Status 0') }, formatter: Table.api.formatter.status },
                        // {
                        //     field: 'countLesson',
                        //     width: "120px",
                        //     title: __('课程管理'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     operate: false,
                        //     buttons: [
                        //         {
                        //             name: 'detail',
                        //             text: __('章节管理'),
                        //             title: __('章节管理'),
                        //             classname: 'btn btn-xs btn-info btn-dialog',
                        //             extend: 'data-area=\'["95%","95%"]\' data-shade=\'[0.5,"#000"]\'',
                        //             icon: 'fa fa-list',
                        //             url: 'classroom/lesson/index',
                        //             callback: function (data) {
                        //                 //;
                        //                 // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                        //             },
                        //             visible: function (row) {
                        //                 console.log('----',row)
                        //                 console.log('----',this.text)
                        //                 this.text = this.title = '章节管理' + row.countLesson
                        //
                        //                 //返回true时按钮显示,返回false隐藏
                        //                 return true;
                        //             }
                        //         },
                        //     ],
                        //     formatter: Table.api.formatter.buttons
                        // },
                        {
                            field: 'countLesson', title: __('章节管理'), operate: false, table: table, class: 'autocontent',
                            formatter: function (value, row, index) {
                                let num = value > 0 ? `<b style="color: #26ffb6;">（${value}）</b>` : '';
                                return `<a href="classroom/lesson/index?ids=${row.id}" class="btn btn-xs btn-info btn-dialog" data-area="[&quot;95%&quot;,&quot;95%&quot;]" data-shade="[0.5,&quot;#000&quot;]" title="章节管理"><i class="fa fa-list"></i> 章节管理${num}</a>`;
                                // return `<span style="color: #1688f1;font-weight: bold;">${value}</span>`;
                            }
                        },
                        {
                            field: 'id_selected',
                            title: __('精品课程'),
                            align: 'center',
                            formatter: Table.api.formatter.toggle,
                            operate: false
                        },
                        { field: 'next_status', title: __('Next_status'), searchList: { "1": __('Next_status 1'), "0": __('Next_status 0') }, formatter: Table.api.formatter.status },
                        {
                            field: 'countStudy', title: __('CountStudy'), operate: false,
                            formatter: function (value, row, index) {
                                return `<a href="classroom/course_record/index?ids=${row.id}" class="btn btn-xs btn-info btn-dialog" data-area="[&quot;95%&quot;,&quot;95%&quot;]" data-shade="[0.5,&quot;#000&quot;]" title="学习人数"><i class="fa fa-group"></i> ${value} 人</a>`;
                            }
                        },
                        {
                            field: 'countMsg', title: __('评论管理'), operate: false,
                            formatter: function (value, row, index) {
                                let num = value > 0 ? `<b style="color: #26ffb6;">（${value}）</b>` : '';
                                return `<a href="classroom/course_msg/index?ids=${row.id}" class="btn btn-xs btn-info btn-dialog" data-area="[&quot;95%&quot;,&quot;95%&quot;]" data-shade="[0.5,&quot;#000&quot;]" title="评论管理"><i class="fa fa-list"></i> 评论管理${num}</a>`;
                            }
                        },
                        { field: 'click', title: __('Click'), operate: false },
                        { field: 'weigh', title: __('Weigh'), operate: false },
                        { field: 'createtime', title: __('Createtime'), visible: false, operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        show: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                // 关联
                $("#c-type_id").change(function () {
                    let v = parseInt($(this).val());
                    let html = ``;
                    if (v > 0) {
                        console.log('c-type_id', v);
                        $.ajax({
                            url: "classroom/type/json",
                            data: { pid: v },
                            type: "post",
                            dataType: "json",
                            success: function (res) {
                                $(res).each(function (k, v) {
                                    html += `<option value="${v.id}">${v.name}</option>`;
                                });
                                $("#c-type2_id").html(html);
                            }
                        });
                    } else {
                        $("#c-type2_id").html(html);
                    }
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});